package me.tuyou.mybatis.test.interfaces.typeHandler;

import me.tuyou.mybatis.test.interfaces.enums.Sex;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SexTypeHandler extends BaseTypeHandler<Sex> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Sex parameter, JdbcType jdbcType) throws SQLException {
        ps.setByte(i, parameter.getCode());
    }

    @Override
    public Sex getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return Sex.fromCode(rs.getByte(columnName));
    }

    @Override
    public Sex getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return Sex.fromCode(rs.getByte(columnIndex));
    }

    @Override
    public Sex getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return Sex.fromCode(cs.getByte(columnIndex));
    }
}
